package com.wuster.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wuster.entity.*;
import com.wuster.exception.OrderException;
import com.wuster.mapper.ProductMapper;
import com.wuster.mapper.UserMapper;
import com.wuster.result.ResultCodeEnum;
import com.wuster.service.OrderService;
import com.wuster.mapper.OrderMapper;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author Admin
* @description 针对表【order】的数据库操作Service实现
* @createDate 2024-07-04 21:14:46
*/
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order>
    implements OrderService{

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private UserMapper userMapper;


    //获取首页订单的统计信息
    @Transactional
    @Override
    public Map<String, Integer> orderInfo() {
        Map<String, Integer> map=new HashMap<>();
        //待发货订单数
        int orderNotsend = orderMapper.selectOrderNotSend();
        map.put("sendOrderNum",orderNotsend);
        //已完成订单数
        int orderFinish = orderMapper.selectOrderFinish();
        map.put("commitOrderNum",(int) orderFinish);
        //商品总数
        long productNum = productMapper.selectCount(null);
        map.put("productNum",(int)productNum);
        //库存告警数
        QueryWrapper<Product>queryWrapper2=new QueryWrapper<>();
        queryWrapper2.eq("storage",0);
        long alarmProductnum = productMapper.selectCount(queryWrapper2);
        map.put("alarmProductnum",(int)alarmProductnum);
        return map;
    }


    //获取订单详情
    @Transactional
    @Override
    public OrderDetailsVO getOrderDetails(int orderId){
        //订单信息
        try {
            Order orderData = orderMapper.getOrderData(orderId);
            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            //
            if(orderData==null){
                return OrderDetailsVO.builder().build();
            }
             if(orderData.getPayTime() != null){
                 orderData.setCreateTime(Timestamp.valueOf(dateFormat.format(orderData.getCreateTime())));
                 orderData.setPayTime(Timestamp.valueOf(dateFormat.format(orderData.getPayTime())));
             }else{
                orderData.setCreateTime(Timestamp.valueOf(dateFormat.format(orderData.getCreateTime())));
            }
            //订单关联的用户信息
            User userData = userMapper.selectById(orderData.getUserId());
            //订单关联的商品信息
            Product productData = productMapper.selectById(orderData.getProductId());
            DecimalFormat df2=new DecimalFormat("#.##");
            OrderDetailsVO orderDetailsVO=OrderDetailsVO.builder()
                    .id(orderData.getId())
                    .userId(orderData.getUserId())
                    .productId(orderData.getProductId())
                    .orderNumber(orderData.getOrderNumber())
                    .createTime(orderData.getCreateTime())
                    .payTime(orderData.getPayTime())
                    .state(orderData.getState())
                    .name(userData.getName())
                    .image(productData.getImage())
                    .productName(productData.getName())
                    .weight(productData.getWeight())
                    .price(productData.getPrice())
                    .build();
            return orderDetailsVO;

        }catch (OrderException e){
            throw new OrderException(ResultCodeEnum.ORDERID_ERROR);
        }

    }


    //获取订单列表分页查询
    @Transactional
    @Override
    public PageInfo<OrderListVO> getOrderList(Map<String,Object>map ){

        int pageSize=(int)map.get("pageSize");
        int pageCurrent=(int)map.get("pageCurrent");


//        //开启分页查询,PageHelper
        PageHelper.startPage(pageCurrent,pageSize);
        List<OrderListVO> orderlistvo = orderMapper.getOrderListBy(map);
        System.out.println("数据数："+orderlistvo.size());
        if(orderlistvo.size()==0 || orderlistvo==null){
            List<OrderListVO> zero = new ArrayList<>();
            return new PageInfo<>(zero);
        }
//        Page<OrderListVO> p=(Page<OrderListVO>) orderlistvo;
//        System.out.println("数据数："+p.getTotal());
//        System.out.println("数据数："+orderlistvo.size());
//        //创建pageBean对象
//        PageBean<OrderListVO> pb = new PageBean<>(p.getTotal(),p.getResult());
//        pb.setTotal(p.getTotal());
//        pb.setItems(p.getResult());
        PageInfo<OrderListVO> pi=new PageInfo<>(orderlistvo);

        return pi;
    }


}




